package cn.ffcs.tmp.web;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.ffcs.common.model.Attach;

import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.EmbeddedObject;
import lotus.domino.Item;
import lotus.domino.NotesException;
import lotus.domino.NotesFactory;
import lotus.domino.NotesThread;
import lotus.domino.RichTextItem;
import lotus.domino.Session;
import lotus.domino.View;

public class DominoServlet extends HttpServlet
{
    private final String ior = "IOR:010000002900000049444c3a6c6f7475732f646f6d696e6f2f636f7262612f494f626a6563745365727665723a312e3000000000010000000000000078000000010101001300000068647a782e666a74656c65636f6d2e636f6d0000f4ea0000310000000438353235363531612d656336382d313036632d656565302d303037653264323233336235004c6f7475734e4f490100010000000100000001000000140000000100000001000105000000000001010000000000";

    public void doGet(HttpServletRequest request, HttpServletResponse res)
            throws IOException, ServletException
    {
        res.setContentType("text/html; charset=gb2312");
        PrintWriter toBrowser = res.getWriter();
        toBrowser.println("<HTML");
        toBrowser.println("<HEAD>");
        toBrowser.println("<TITLE>Example 2</TITLE>");
        toBrowser.println("</HEAD>");
        toBrowser.println("<BODY>");
        toBrowser.println("<H1>Example 2</H1>");
        try
        {
            Session session = NotesFactory.createSessionWithIOR(ior);
            // NotesFactory.createSession("134.128.35.137:60148","admin","");
            // //*创建新会话对象
            // 获取数据库及日志参数
            toBrowser.println("<H1>session:" + session + "</H1>");
            Database oadb = session.getDatabase(null, "dx_yunyzxjk.nsf");
            toBrowser.println("<H1>Database:" + oadb + "</H1>");
            View view = oadb.getView("Yunyzxjk");
            Document doc = view.getFirstDocument();
            toBrowser.println(oadb.getTitle());
            toBrowser.println("<table>");
            String tmp="";

            File rFile=new File("/local/notesdata/dx_yunyzxjk.nsf");
            toBrowser.println("rFile.getAbsolutePath():"+rFile.getAbsolutePath());
            toBrowser.println("rFile.getPath():"+rFile.getPath());
            
            while (doc != null)
            {
                toBrowser.println("<tr>");
                toBrowser.println("<td>");
                toBrowser.println(doc.getItemValueString("Biaot"));
                toBrowser.println("</td>");
                toBrowser.println("<td>");
                toBrowser.println(doc.getItemValueString("Fujm"));
                toBrowser.println("</td>");
                toBrowser.println("<td>");
                toBrowser.println(doc.getItemValueString("Body"));
                toBrowser.println("</td>");
                toBrowser.println("<td>");
                toBrowser.println(doc.getItemValueString("Jiekbz"));
                toBrowser.println("</td>");
                toBrowser.println("<td>");
                toBrowser.println(doc.getItemValueString("Zhuangr"));
                toBrowser.println("</td>");
                toBrowser.println("</tr>");
                Vector fileNames = session.evaluate("@AttachmentNames",doc);
                
                RichTextItem aa=(RichTextItem)doc.getFirstItem("Body");
                Vector objs=aa.getEmbeddedObjects();
                EmbeddedObject eo;
                
              for (int i = 0; i < objs.size(); i++){
                  eo=(EmbeddedObject)objs.get(i);
                  toBrowser.println("eo.getName():"+eo.getName());
                  
              }
              Item item=doc.getFirstItem("Fujm");
              objs=item.getValues();
              
                for (int i = 0; i < objs.size(); i++){
                    toBrowser.println("eo.getName():"+objs.get(i));
                    
                }
                
//                for (int i = 0; i < objs.size(); i++){
//                    eo=(EmbeddedObject)objs.get(i);
//                    toBrowser.println("eo.getName():"+eo.getName());
//                    
//                    InputStream input =eo.getInputStream();
//                    ByteArrayOutputStream os = new ByteArrayOutputStream();
//                    
//                    int len=64*1024;
//                    int count=-1;
//                    byte[] buffer = new byte[len];
//                    while((count=input.read(buffer,0,len))>-1){
//                        os.write(buffer,0,count);
//                    }
//                    byte a[] = os.toByteArray();
////                    toBrowser.println("a:"+new String(a));
//                    input.close();
//                    os.flush();
//                    os.close();
//                }

                doc = view.getNextDocument(doc);
            }
            toBrowser.println("</table>");
            view.recycle();
            oadb.recycle();
            session.recycle();
        }
        catch (NotesException n)
        {
            n.printStackTrace();
            toBrowser.println("<H1>n:" + n + "</H1>");
        }
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException
    {
        this.doGet(request, response);
    }
}
